#include "Widget.hpp"

#include "Core/Cell.hpp"
#include "Core/RuleData.hpp"
#include "Core/SquareGrid.hpp"
#include "Core/HexagonGrid.hpp"

#include <QtCore/QDebug>
#include <QtCore/QTimer>

#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>

Widget::Widget( QWidget* parent )
    : QWidget( parent )
    , Rules( new RuleData() )
    , TestGrid( new HexagonGrid( 5u, 5u, this ) )
{
    QVBoxLayout * mainLayout = new QVBoxLayout( this );
    setLayout( mainLayout );

    button = new QPushButton( tr( "initialise" ), this );
    mainLayout -> addWidget( button );

    connect( button, SIGNAL( clicked() ), this, SLOT( initMat() ) );

    Rules -> addBirthCondition( 3u );
    Rules -> addSurvivalCondition( 2u );
    Rules -> addSurvivalCondition( 3u );

    TestGrid -> setGameRules( Rules );
}

Widget::~Widget()
{
    delete Rules;
}

void Widget::initMat()
{
    QTimer::singleShot( 0 , TestGrid, SLOT( initialise() ) );

    disconnect( button, SIGNAL( clicked() ), this, SLOT( initMat() ) );

    button -> setText( tr( "toggle status" ) );

    connect( button, SIGNAL( clicked() ), this, SLOT( initGrid() ) );
}

void Widget::initGrid()
{
    // still life - Block
    /*
    QTimer::singleShot( 0 , TestGrid -> operator()( 1u, 1u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 2u, 1u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 1u, 2u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 2u, 2u ), SLOT( toggleCurrentStatus() ) );
    */
    // still life - beehive
    QTimer::singleShot( 0 , TestGrid -> operator()( 1u, 2u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 1u, 3u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 2u, 1u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 2u, 4u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 3u, 2u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 3u, 3u ), SLOT( toggleCurrentStatus() ) );
    // oscillator - Blinker (not working)
    /*
    QTimer::singleShot( 0 , TestGrid -> operator()( 0u, 1u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 1u, 1u ), SLOT( toggleCurrentStatus() ) );
    QTimer::singleShot( 0 , TestGrid -> operator()( 2u, 1u ), SLOT( toggleCurrentStatus() ) );
    */


    disconnect( button, SIGNAL( clicked() ), this, SLOT( initGrid() ) );

    button -> setText( tr( "step" ) );

    connect( button, SIGNAL( clicked() ), this, SLOT( callStep() ) );
}

void Widget::callStep()
{
    qDebug() << "------- step -------";
    QTimer::singleShot( 0, TestGrid, SLOT( step() ) );
}
